import InteractiveBase from "../../../LevelCommon/InteractiveBase";
import LevelBaseControl from "../../../LevelCommon/LevelBaseControl";
import { GamePropData } from "../../../Data/GamePropData";
import GamePropConst from "../../../../constant/GamePropConst";
import CommonConstant from "../../../../constant/CommonConstant";
import SparkPlug from "./SparkPlug";
import { utils } from "../../../../utils/CommonUtil";

export default class SparkPlugLockKey extends InteractiveBase {
    /** 当前需要的火花塞id*/
    public curNeedPlugId: number;
    /** 当前放上的火花塞id*/
    private curSparkId: number = 0;
    /** 火花塞的位置*/
    private sparkPos: Laya.Sprite3D;

    private sparkPlugLock: any;

    private curSelectId: number;

    private sparkObj:Laya.Sprite3D;

    private get CurSparkId(): number {
        //  get
        //  {
        /** 没装火花塞  返回0*/
        if (this.sparkPos.numChildren == 0) {
            return 0;
        }
        else {
            return this.curSparkId;
        }
    }

    private set CurSparkId(value: number) {
        this.curSparkId = value;
    }

    public SetData(sparkPlugLock: any) {
        this.sparkPlugLock = sparkPlugLock;
    }
    onStart() {
        this.sparkPos = this.owner.getChildByName("sparkPos") as Laya.Sprite3D;
    }
    protected InteractiveLogic(): boolean {
        /** 如果当前有火花塞就直接返回*/
        if ((this.sparkPlugLock.isCorrect || this.sparkPos.numChildren != 0)&& this.sparkPos.getChildAt(0).activeInHierarchy && 
        this.sparkPos.getChildAt(0).getChildAt(0).activeInHierarchy ) {
            return false;
        }
        let curLevelControl: LevelBaseControl = window.gameFacade.LevelMng.GetCurLevelControl();
        let curSelectProp: GamePropData = curLevelControl.CurSelectProp;
        this.curSelectId = -1;
        if (curSelectProp != null) {
            this.curSelectId = curSelectProp.id;
        }
        /** 如果选择了火花塞*/
        if (this.curSelectId == GamePropConst.CIRCLE_SPARK_PLUG ||
            this.curSelectId == GamePropConst.CUBE_SPARK_PLUG ||
            this.curSelectId == GamePropConst.TRAPEZOID_SPARK_PLUG ||
            this.curSelectId == GamePropConst.DIAMOND_SPARK_PLUG) {
            curLevelControl.UseCurProp();
            /** 生成火花塞物体*/
            let sparkPlugPath = CommonConstant._sparkPlugs + this.curSelectId +".lh";
            window.gameFacade.ResourceMng.LoadSprite3D<Laya.Sprite3D>(
                sparkPlugPath,
                this.onSparkPlugLoadSucc, this);

        }
        return false;
    }

    private onSparkPlugLoadSucc(sparkObj: Laya.Sprite3D) {
        this.sparkObj = sparkObj;
        let targetSparkObj = sparkObj.getChildAt(0) as Laya.Sprite3D;
        targetSparkObj.active = true;
        this.sparkPos.addChild(sparkObj);
        let SparkPlug_targetSparkObj = utils.CommonUtils.CustomAddComponent(targetSparkObj,SparkPlug) as SparkPlug;
        SparkPlug_targetSparkObj.gamePropId = this.curSelectId;

        sparkObj.transform.localPosition = new Laya.Vector3(0, 0, 0);
        sparkObj.transform.localScale = new Laya.Vector3(1, 1, 1);
        // sparkObj.transform.localRotationEuler = new Laya.Vector3(0,0,0);

        this.CurSparkId = this.curSelectId;
        this.sparkPlugLock.CheckCorrect();
        /** 重新设置子交互物体  因为新生成了东西*/
        if (this.parentInteractive != null) {
            this.parentInteractive.SetAllSonObj();
        }
    }
    /** 当前火花塞是否正确*/
    public IsCorrect(): boolean {
        return this.CurSparkId == this.curNeedPlugId;
    }

    onDestroy(){
        // this.sparkObj.destroy();
    }
}